package com.quectel.business.common.grid;

import cn.hutool.core.date.DateTime;
import com.quectel.constant.OrderCodePrefixConstants;
import com.quectel.constant.core.grid.GridConstant;
import com.quectel.constant.core.grid.GridPlanTaskConstant;
import com.quectel.core.module.grid.dto.GridDto;
import com.quectel.core.module.grid.dto.GridEventRecordDto;
import com.quectel.core.module.grid.dto.GridEventRecordLogDto;
import com.quectel.core.module.grid.dto.GridMemberDto;
import com.quectel.util.ex.CheckErrorException;
import com.quectel.util.kit.OrderUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangheng
 * @email nolan.zhang@quectel.com
 * @date 2023/2/17 16:54
 * @description new java file header...
 */
@Component
public class GridEventRecordBusiness {
    @Autowired
    private GridBusiness gridBusiness;

    /**
     * 停车月卡订购参数校验
     *
     * @param gridEventRecordDto
     */
    public void checkEventParams(GridEventRecordDto gridEventRecordDto, GridMemberDto gridMemberDto) {

        if (gridEventRecordDto.getGridId() == null) {
            throw new CheckErrorException("网格信息不能为空");
        }
        GridDto gridDto = gridBusiness.checkGridMemberPermission(gridMemberDto.getId(), gridEventRecordDto.getGridId());
        if (gridDto == null) {
            throw new CheckErrorException("未查询到网格信息");
        }

    }

    public GridEventRecordDto genReportEvent(GridEventRecordDto gridEventRecordDto, GridDto gridDto, GridMemberDto gridMember) {
        //填充信息
        gridEventRecordDto.setTenantId(gridDto.getTenantId());
        gridEventRecordDto.setVillageId(gridDto.getVillageId());
        gridEventRecordDto.setGridMemberId(gridMember.getId());
        gridEventRecordDto.setCreatorLevel(GridConstant.RegionLevel.GRID_LEVEL.getLevel());
        gridEventRecordDto.setCreatorUniqueFlag(String.valueOf(gridDto.getId()));
        gridEventRecordDto.setSourceType(GridConstant.GridEventRecordSourceType.REPORT.getType());
        gridEventRecordDto.setEventNo(OrderUtils.getOrderNo(OrderCodePrefixConstants.GRID_EVENT_RECORD_PREFIX));
        gridEventRecordDto.setCreator(gridDto.getName());
        gridEventRecordDto.setCreateTime(DateTime.now());

        List<GridEventRecordLogDto> logs = new ArrayList<>();

        //如果自行处置不为空 则证明为自行处置
        if (StringUtils.isNotEmpty(gridEventRecordDto.getSelfHandleResult())) {
            //状态为待办结
            gridEventRecordDto.setStatus(GridConstant.GridEventRecordStatus.TO_BE_FINISHED.getStatus());
            gridEventRecordDto.setEventType(GridPlanTaskConstant.ReportEventTypeEnum.OWN.getStatus());

            GridEventRecordLogDto selfHandleLog = new GridEventRecordLogDto();
            selfHandleLog.setHandleLevel(GridConstant.RegionLevel.GRID_LEVEL.getLevel());
            selfHandleLog.setHandleStatus(GridConstant.GridEventRecordLogType.FEEDBACK.getStatus());
            selfHandleLog.setHandleUnitName(gridDto.getVillage().getName() + gridDto.getName());
            selfHandleLog.setHandleUserType(GridConstant.GridEventRecordPersonType.GRID_MEMBER.getType());
            selfHandleLog.setHandleUserId(gridMember.getId());
            selfHandleLog.setHandleDetail(gridEventRecordDto.getSelfHandleResult());
            selfHandleLog.setHandlePicturesJson(gridEventRecordDto.getSelfHandlePicturesJson());
            selfHandleLog.setHandleDesc("由\"" + gridMember.getName() + "\"自行处置");
            selfHandleLog.setCreateTime(DateTime.now());
            logs.add(selfHandleLog);
        } else {

            //状态为待处理
            gridEventRecordDto.setStatus(GridConstant.GridEventRecordStatus.TO_BE_PROCESSED.getStatus());
            gridEventRecordDto.setEventType(GridPlanTaskConstant.ReportEventTypeEnum.SUPERIORS.getStatus());

            GridEventRecordLogDto reportLog = new GridEventRecordLogDto();
            reportLog.setHandleLevel(GridConstant.RegionLevel.GRID_LEVEL.getLevel());
            reportLog.setHandleStatus(GridConstant.GridEventRecordLogType.REPORT.getStatus());
            reportLog.setHandleUnitName(gridDto.getVillage().getName() + gridDto.getName());
            reportLog.setHandleUserType(GridConstant.GridEventRecordPersonType.GRID_MEMBER.getType());
            reportLog.setHandleUserId(gridMember.getId());
            reportLog.setHandleDesc("由\"" + gridMember.getName() + "\"上报");
            reportLog.setCreateTime(DateTime.now());
            logs.add(reportLog);
        }

        //处理节点到小区
        gridEventRecordDto.setCurrentCode(String.valueOf(gridDto.getVillage().getId()));
        gridEventRecordDto.setCurrentLevel(GridConstant.RegionLevel.VILLAGE_LEVEL.getLevel());
        gridEventRecordDto.setLogList(logs);
        return gridEventRecordDto;
    }

}
